﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Reflection;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.EventRef"/>.
    /// </summary>
    public class EventRefVM : VariableRefVM
    {
        #region /* STATICS */

        internal static void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(EventRef),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new EventRefVM((EventRef)codeObject, dictionary); });
        }

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.EventRef"/>.
        /// </summary>
        public EventRefVM(EventRef eventRef, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(eventRef, dictionary)
        { }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.EventRef"/> model.
        /// </summary>
        public EventRef EventRef
        {
            get { return (EventRef)CodeObject; }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        public static void RenderEventInfo(CodeRenderer renderer, EventInfo eventInfo, RenderFlags flags, CodeObjectVM tag)
        {
            RenderFlags passFlags = flags & ~RenderFlags.Description;
            bool hasBorder = flags.HasFlag(RenderFlags.ForceBorder);
            if (hasBorder)
                renderer.CreateBorder(PropertyDeclBaseVM.StaticBorderBrush, PropertyDeclBaseVM.StaticBackgroundBrush, tag);

            if (!flags.HasFlag(RenderFlags.NoPreAnnotations))
                AttributeVM.RenderAttributes(renderer, eventInfo, tag);
            renderer.RenderText(ModifiersHelpers.AsString(EventRef.GetEventModifiers(eventInfo)), KEYWORD_BRUSH, tag);
            TypeRefBaseVM.RenderTypeAsTypeRefVM(renderer, eventInfo.EventHandlerType, passFlags, tag);  // Render as TypeRefVM for nested tooltips
            renderer.RenderText(" ", PUNC_BRUSH, tag);
            TypeRefBaseVM.RenderTypeAsTypeRefVM(renderer, eventInfo.DeclaringType, passFlags, tag);  // Render as TypeRefVM for nested tooltips
            renderer.RenderText(Dot.ParseToken, PUNC_BRUSH, tag);
            renderer.RenderText(eventInfo.Name, IDENTIFIER_BRUSH, tag);

            // If we have a border, return to the previous one
            if (hasBorder)
                renderer.ReturnToBorderParent(tag);
        }

        #endregion
    }
}
